package com.mf.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Redis工具类
 *
 * @author: Jiang Runqing
 * @date: 2018-11-01 23:58
 */
@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**************************common*************************/

    /**
     * desription: 指定缓存失效时间
     *
     * @params: [key, time] key 键   time时间
     * @return: boolean
     * @author: Jiang Runqing
     * @date: 2018/11/2 00:03
     */
    public boolean expire(String key, long time) {

        try {
            if (time > 0) {
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * desription:根据key 获取过期时间
     *
     * @params: [key]键 not null
     * @return: long 时间（s）返回0则 永不过期
     * @author: Jiang Runqing
     * @date: 2018/11/2 00:06
     */
    public long getExpire(String key) {
        return redisTemplate.getExpire(key);
    }

    /**
     * desription: 判断key是否存在
     *
     * @params: [key]
     * @return: boolean true 存在   false 不存在
     * @author: Jiang Runqing
     * @date: 2018/11/2 00:09
     */
    public boolean hasKey(String key) {
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * desription: 删除缓存
     *
     * @params: [key] 一个或多个值
     * @return: void
     * @author: Jiang Runqing
     * @date: 2018/11/2 00:14
     */
    public void delete(String... key) {
        if (null != key && key.length > 0) {
            if (key.length == 1) {
                redisTemplate.delete(key[0]);
            } else {
                redisTemplate.delete(CollectionUtils.arrayToList(key));
            }
        }
    }

    /**************************String*************************/


    /**
     * desription: 普通缓存获取
     *
     * @params: [key]
     * @return: java.lang.Object
     * @author: Jiang Runqing
     * @date: 2018/11/2 00:17
     */
    public Object get(String key) {
        if (null == key) {
            return null;
        }
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * desription: 普通缓存设置
     *
     * @params: [key, value]
     * @return: boolean
     * @author: Jiang Runqing
     * @date: 2018/11/2 00:18
     */
    public boolean set(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * desription: 带时间的普通缓存设置
     *
     * @params: [key, value, time]key 键 value 值  time时间（秒） time时间<=0则设置永久缓存
     * @return: boolean
     * @author: Jiang Runqing
     * @date: 2018/11/2 00:24
     */
    public boolean set(String key, Object value, long time) {
        try {
            if (time > 0) {
                redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
            } else {
                set(key, value);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * desription: 递增
     *
     * @params: [key, delta] delta 要增加几（>0）
     * @return: long
     * @author: Jiang Runqing
     * @date: 2018/11/2 00:29
     */
    public long incr(String key, long delta) {
        if (delta < 0) {
            throw new RuntimeException("递增因子delta必须大于0");
        }
        return redisTemplate.opsForValue().increment(key, delta);
    }

    /**
     * desription: 递减
     *
     * @params: [key, delta] delta 要减少几（>0）
     * @return: long
     * @author: Jiang Runqing
     * @date: 2018/11/2 00:33
     */
    public long decr(String key, long delta) {
        if (delta < 0) {
            throw new RuntimeException("递减因子delta必须大于0");
        }
        return redisTemplate.opsForValue().increment(key, -delta);
    }

    /**************************Map***************************/

    /**
     * desription: HashGet
     *
     * @params: [key, item] key item not null
     * @return: java.lang.Object
     * @author: Jiang Runqing
     * @date: 2018/11/2 00:38
     */
    public Object hget(String key, String item) {
        return redisTemplate.opsForHash().get(key, item);
    }

    /**
     * desription:  获取hashKey对应的所有键值
     *
     * @params: [key]
     * @return: 对应多个值
     * @author: Jiang Runqing
     * @date: 2018/11/2 00:39
     */
    public Map<Object, Object> hmget(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     * desription: HashSet
     *
     * @params: [key, map] map 对应多个值
     * @return: boolean
     * @author: Jiang Runqing
     * @date: 2018/11/2 00:43
     */
    public boolean hmset(String key, Map<String, Object> map) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * desription: 带时间的HashSet
     *
     * @params: [key, map, time]key键 map对应多个键值 time时间（s）
     * @return: boolean
     * @author: Jiang Runqing
     * @date: 2018/11/2 00:45
     */
    public boolean hmset(String key, Map<String, Object> map, long time) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * desription: 向一张hash表中放入数据,如果不存在将创建
     *
     * @params: [key, item, value] key键 item项 value值
     * @return: boolean
     * @author: Jiang Runqing
     * @date: 2018/11/2 00:48
     */
    public boolean hset(String key, String item, Object value) {
        try {
            redisTemplate.opsForHash().put(key, item, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * desription:向一张hash表中放入数据,如果不存在将创建
     * time: 时间(s),如果hash表已存在时间，则会替换原有时间
     *
     * @params: [key, item, value, time]
     * @return: boolean
     * @author: Jiang Runqing
     * @date: 2018/11/2 00:52
     */
    public boolean hset(String key, String item, Object value, long time) {
        try {
            redisTemplate.opsForHash().put(key, item, value);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * desription: 删除Hash表中的值
     *
     * @params: [key, item] item可以为多上，not null
     * @return: void
     * @author: Jiang Runqing
     * @date: 2018/11/2 00:56
     */
    public void hdel(String key, Object... item) {
        redisTemplate.opsForHash().delete(key, item);
    }

    /**
     * desription:判断hash表中是否有该项的值
     *
     * @params: [key, item] all not null
     * @return: boolean
     * @author: Jiang Runqing
     * @date: 2018/11/2 00:58
     */
    public boolean hHasKey(String key, String item) {
        return redisTemplate.opsForHash().hasKey(key, item);
    }

    /**
     * desription: hash递增，如果不存在，就是创建一个，并且把新增的值返回
     *
     * @params: [key, item, delta] key键 item项 delta要增加几(>0)
     * @return: double
     * @author: Jiang Runqing
     * @date: 2018/11/2 01:03
     */
    public double hincr(String key, String item, double delta) {
        return redisTemplate.opsForHash().increment(key, item, delta);
    }

    /**
     * desription: hash递减
     *
     * @params: [key, item, delta] key键 item项 delta要减去几(>0)
     * @return: double
     * @author: Jiang Runqing
     * @date: 2018/11/2 01:05
     */
    public double hdecr(String key, String item, double delta) {
        return redisTemplate.opsForHash().increment(key, item, -delta);
    }

    /**************************Set*************************/

    /**
     * desription: 根据key获取Set中所有的值
     *
     * @params: [key]键
     * @return: java.util.Set<java.lang.Object>
     * @author: Jiang Runqing
     * @date: 2018/11/3 00:27
     */
    public Set<Object> sGet(String key) {
        try {
            return redisTemplate.opsForSet().members(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * desription: 根据value从一个set中查询是否存在
     *
     * @params: [key, value] key键 value值
     * @return: boolean
     * @author: Jiang Runqing
     * @date: 2018/11/3 00:30
     */
    public boolean sHaskey(String key, Object value) {
        try {
            return redisTemplate.opsForSet().isMember(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * desription: 将数据放入set缓存
     *
     * @params: [key, values]key键 values值 可为多个
     * @return: long 成功的个数
     * @author: Jiang Runqing
     * @date: 2018/11/3 00:32
     */
    public long sSet(String key, Object... values) {
        try {
            return redisTemplate.opsForSet().add(key, values);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * desription: 将带时间的数据放入set缓存
     *
     * @params: [key, time, values]key键 time时间(秒) values值 可以为多个
     * @return: long
     * @author: Jiang Runqing
     * @date: 2018/11/3 00:36
     */
    public long sSet(String key, long time, Object... values) {
        try {
            Long count = redisTemplate.opsForSet().add(key, values);
            if (time > 0) {
                expire(key, time);
            }
            return count;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * desription: 获取set缓存的长度
     *
     * @params: [key]
     * @return: long
     * @author: Jiang Runqing
     * @date: 2018/11/3 00:39
     */
    public long sGetSetSize(String key) {
        try {
            return redisTemplate.opsForSet().size(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * desription: 移除值为value的set
     *
     * @params: [key, values]key键 values值 可为多个
     * @return: long 移除的个数
     * @author: Jiang Runqing
     * @date: 2018/11/3 00:41
     */
    public long setRemove(String key, Object... values) {
        try {
            Long count = redisTemplate.opsForSet().remove(key, values);
            return count;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**************************List*************************/

    /**
     * desription: 获取list缓存的内容
     * 0到 -1代表所有值
     *
     * @params: [key, start, end] key键 start开始 end结束
     * @return: java.util.List<java.lang.Object>
     * @author: Jiang Runqing
     * @date: 2018/11/3 00:44
     */
    public List<Object> lGet(String key, long start, long end) {
        try {
            return redisTemplate.opsForList().range(key, start, end);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * desription: 获取list缓存的长度
     *
     * @params: [key]
     * @return: long
     * @author: Jiang Runqing
     * @date: 2018/11/3 00:47
     */
    public long lGetListSize(String key) {
        try {
            return redisTemplate.opsForList().size(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * desription: 通过索引获取list中的值
     * index索引
     * index>=0 0表头 1第二个元素，依次类推；
     * index<0  -1表尾 -2倒数第二个元素，依次类推
     *
     * @params: [key, index] key键
     * @return: java.lang.Object
     * @author: Jiang Runqing
     * @date: 2018/11/3 00:49
     */
    public Object lGetIndex(String key, long index) {
        try {
            return redisTemplate.opsForList().index(key, index);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * desription: 将list放入缓存
     *
     * @params: [key, value]key键 value值
     * @return: boolean
     * @author: Jiang Runqing
     * @date: 2018/11/3 00:55
     */
    public boolean lSet(String key, Object value) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * desription: 将带时间的list放入缓存
     *
     * @params: [key, value, time]key键 value值 time时间(s)
     * @return: boolean
     * @author: Jiang Runqing
     * @date: 2018/11/3 00:56
     */
    public boolean lSet(String key, Object value, long time) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * desription:将list放入缓存
     *
     * @params: [key, value]
     * @return: boolean
     * @author: Jiang Runqing
     * @date: 2018/11/3 00:59
     */
    public boolean lSet(String key, List<Object> value) {
        try {
            redisTemplate.opsForList().rightPushAll(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * desription: 将带时间的list放入缓存
     *
     * @params: [key, value, time]
     * @return: boolean
     * @author: Jiang Runqing
     * @date: 2018/11/3 01:00
     */
    public boolean lSet(String key, List<Object> value, long time) {
        try {
            redisTemplate.opsForList().rightPushAll(key, value);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * desription: 根据索引修改list中的某条数据
     *
     * @params: [key, index, value]key键 index索引 value要修改的值
     * @return: boolean
     * @author: Jiang Runqing
     * @date: 2018/11/3 01:02
     */
    public boolean lUpdateIndex(String key, long index, Object value) {
        try {
            redisTemplate.opsForList().set(key, index, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * desription: 移除N个值为value
     *
     * @params: [key, count, value] key键 count移除多少个 value值
     * @return: long 移除的个数
     * @author: Jiang Runqing
     * @date: 2018/11/3 01:06
     */
    public long lRemove(String key, long count, Object value) {
        try {
            long removeCount = redisTemplate.opsForList().remove(key, count, value);
            return removeCount;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

}
